const path=require('path')
const fs=require('fs')
const dbpath=path.join(__dirname,'../','/pull','db','class.json')

//查询
const getclass=async(params)=>{
let res=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
return res
}

//修改
const edclass=async(params)=>{
let dbdata=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
let newdata=[...dbdata]
newdata.forEach(item=> {
    if(item.id==params.id){
     item.className=params.className
     item.classNum=params.classNum
     item.level=params.level
    }
});
fs.writeFileSync(dbpath,JSON.stringify(dbdata))
return true
}


//删除
const delclass=async(params)=>{
 const {id}=params
let dbdata=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
let newdata=[...dbdata]
let data=[]
newdata.forEach(item=>{
    if(item.id!=id){
     data.push(item)
    }
})
fs.writeFileSync(dbpath,JSON.stringify(dbdata))
return true
}


//增加
const addclass=async(params)=>{
    const {id,className,classNum,level}=params
    let dbdata=JSON.parse(fs.readFileSync(dbpath,'utf-8'))
    dbdata.push(params)
    fs.writeFileSync(dbpath,JSON.stringify(dbdata))
    return true
}



module.exports={
    getclass,
     edclass,
    delclass,
    addclass
}